<?php 
/*
 module:		基本配置控制器
 create_time:	2022-08-09 17:31:39
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\BaseconfigModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class BaseconfigController extends CommonController {


	/*
 	* @Description  基本配置
 	*/
	public function index(){
		if($this->request->isMethod('get')){
			return view('admin/baseconfig/index');
		}else{
			$data = $this->request->all();
			$validate = new \App\Validate\admin\BaseconfigValidate;
			if(!$validate->scene('index')->check($data)){
				throw new ValidateException ($validate->getError());
			}

		    $data['keyword'] = implode(',',$data['keyword']);

			$info = BaseconfigModel::pluck('data','name')->toArray();
			foreach ($data as $key => $value) {
				if(array_key_exists($key,$info)){
					BaseconfigModel::select('data')->where(['name'=>$key])->update(['data'=>$value]);
				}else{
					BaseconfigModel::insert(['name'=>$key,'data'=>$value]);
				}
			}
			return json(['status'=>200,'msg'=>'操作成功']);
		}
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getInfo(){
		$res = BaseconfigModel::pluck('data','name');
		$res['keyword'] = explode(',',$res['keyword']);
		$res['water_alpha'] = (int)$res['water_alpha'];

		$data['status'] = 200;
		$data['data'] = $res;
		return json($data);
	}






}



